package com.lwh.service.impl;

import com.lwh.common.uniqueuekey.IdWorker;
import com.lwh.dao.MdCheliangMapper;
import com.lwh.entity.MdCheliangEntity;
import com.lwh.service.MdCheliangService;
import com.lwh.util.EasyPoiUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import java.util.*;


@Service
public class MdCheliangServiceImpl  implements MdCheliangService {

    @Autowired
    private MdCheliangMapper mdCheliangMapper;



    @Override
    public Map<String, Object> findPage(MdCheliangEntity query)  {
        int total = mdCheliangMapper.findPageCount(query);
        List<MdCheliangEntity> list = mdCheliangMapper.findPage(query);
        Map<String, Object> map = new HashMap<String,Object>();
        map.put("total", total);
        map.put("rows", list);
        return map;

    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public int insertSelective(MdCheliangEntity mdCheliangEntity) {
        IdWorker idWorker=new IdWorker(0,0);
        String id = String.valueOf(idWorker.nextId());
        mdCheliangEntity.setId(id);
        mdCheliangEntity.setCreateDate(new Date());
        mdCheliangMapper.insertSelective(mdCheliangEntity);

        return 1;
    }

    @Override
    public int updateByPrimaryKeySelective(MdCheliangEntity mdCheliangEntity) {
        return mdCheliangMapper.updateByPrimaryKeySelective(mdCheliangEntity);
    }

    @Override
    public int deleteByIds(String[] ids) {
        return mdCheliangMapper.deleteMore(ids);
    }

    @Override
    public int deleteOneById(String id) {
        return mdCheliangMapper.deleteOne(id);
    }

    @Override
    public String importExcel(MultipartFile uploadFile) {
        List<MdCheliangEntity> mdList = new ArrayList<MdCheliangEntity>();
        IdWorker idWorker=new IdWorker(0,0);
        String result = null;
        try {
            String filename = uploadFile.getOriginalFilename();
            if(filename != null && (filename.toLowerCase().endsWith(".xls") || filename.toLowerCase().endsWith(".xlsx"))){
                List<MdCheliangEntity> list = EasyPoiUtil.importExcel(uploadFile,1,1,MdCheliangEntity.class);
                for(MdCheliangEntity md : list){
                   MdCheliangEntity mdpo = new MdCheliangEntity();
                   String id = String.valueOf(idWorker.nextId());
                   mdpo.setId(id);
                   mdpo.setCreateDate(new Date());
                   mdpo.setChepaihao(md.getChepaihao());
                   mdpo.setChexing(md.getChexing());
                   mdpo.setZuidatiji(md.getZuidatiji());
                   mdpo.setZaizhong(md.getZaizhong());
                   mdpo.setZairen(md.getZairen());
                   mdpo.setJiazhao(md.getJiazhao());
                   mdpo.setZhuangtai(md.getZhuangtai());
                   mdpo.setBeizhu(md.getBeizhu());
                   mdpo.setUsername(md.getUsername());
                   mdpo.setGpsid(md.getGpsid());
                   mdpo.setQuyu(md.getQuyu());
                   mdList.add(mdpo);
                }
                mdCheliangMapper.importExcel(mdList);
                result = "success";
            }else{
                result = "导入的文件不是excel文件";
            }

        } catch (Exception e) {
            e.printStackTrace();
            result = "fail";
        }finally {

        }
        return result;
    }

    @Override
    public List<MdCheliangEntity> outExcel(MdCheliangEntity mdCheliangEntity) {

        List<MdCheliangEntity> list = mdCheliangMapper.outExcel(mdCheliangEntity);
        return list;
    }

    @Override
    public List<MdCheliangEntity> findAll() throws Exception {
        return null;
    }

}
